<template>
    <div class='crematedScane'>
        <div class='content'>
            <div v-for="(item,index) in msg" :key="index" :class="item.class">
                {{item.text}}
            </div>
        </div>
        <div class='footer'>
            <div class='sn'>
                <el-input v-model="input" placeholder="请扫描二维码" autofocus=true @keyup.enter.native="sance()" id="search"></el-input>
            </div>
            <div class='button'>
                <el-button type="danger" @click="closes()">关闭</el-button>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            appid:"",
            input:'',
            step:1,
            type:"start",
            sCode:"",//工号
            sCode2:"",//炉号
            sCode3:"",//逝者编号
            href:"",
            isLQ:false,
            msg:[{
                text:"请扫描逝者二维码开始火化...",
                class:"item"
            }]
        }
    },
    created(){
        this.href = window.location.href; /* 获取完整URL */
        let sef=this;
         if(this.$route.query.appid){
            this.appid=this.$route.query.appid;
        }
       /* window.onkeydown=function(e){
            let key=window.event.keyCode;

            if(key==13){
                sef.sance();
            }else if((key>=48&&key<=57)||(key>=65&&key<=90)){
                sef.input=sef.input+String.fromCharCode(key);
            }
                
        }*/
        window.setInterval(function(){
            document.getElementById("search").focus();
        },5000)
        
    },
    watch:{
        msg:{//深度监听，可监听到对象、数组的变化
            handler(val, oldVal){
                this.$nextTick(() => {
                    var div = document.getElementsByClassName('content')[0];
                    div.scrollTop = div.scrollHeight;
                    
                });
            },
            deep:true //true 深度监听
        }
    },
    methods:{
        sance(){
            if(this.input=="111"){
                let data={};
               // data.text="请扫描炉号二维码已完成火化...";
                data.text="请扫描逝者二维码已完成火化...";
                data.class="item item_hr"
                this.msg.push(data);
                this.type="end";
            }else{
                if(this.type=="end"){
                    let sCode=this.input;
                    let data_={};
                    data_.text="逝者二维码："+sCode;
                    data_.class="item2"
                    this.msg.push(data_);
                    data_={};
                    data_.text="正在进行火化调度，请稍等...";
                    data_.class="item2"
                    this.msg.push(data_);
                    this.$post(this.appid,"byg/cremated/EndCremated2",{ids:sCode}).then(data=>{
                        this.type="start";
                        let data_={};
                        data_.text=unescape(data[0].msg);
                        if(data[0].stauts=="+OK"){
                            data_.class="item3";
                            
                            this.msg.push(data_);

                            data_={};
                            data_.type="goSearch";
                            data_.url=this.href;
                            //parent.postMessage(JSON.stringify(data_),'*');
                            this.$emit("goSearch",JSON.stringify(data_))

                            this.step==1;
                            data_={};
                            data_.text="请扫描逝者二维码开始火化...";
                            data_.class="item item_hr"
                            this.msg.push(data_);

                            this.sCode="";
                        }else{
                            data_.class="item3";
                            this.msg.push(data_);
                        }
                            

                       
                    });
                }else{
                    if(this.isLQ){
                        this.isLQ=false;
                        this.sCode=this.input;
                        let data_={};
                        data_.text="骨灰盒标签条码："+this.sCode3;
                        data_.class="item2"
                        this.msg.push(data_);

                        if(this.sCode!=this.sCode3){
                            data_={};
                            data_.text="骨灰盒标签核对失败，请仔细检查";
                            data_.class="item3"
                            this.msg.push(data_);

                            this.sCode="";
                            this.sCode3="";
                        }else{
                            data_={};
                            data_.text="正在进行取灰操作，请稍等...";
                            data_.class="item2"
                            this.msg.push(data_);

                            this.$post(this.appid,"byg/ashes/saveAshes",{
                                sCode:this.sCode
                            }).then(data=>{
                                data_={};
                                data_.text=unescape(data[0].msg);
                                data_.class="item3"
                                this.msg.push(data_);
                                this.sCode="";
                                this.sCode3="";

                                data_={};
                                data_.text="请扫描逝者二维码开始火化...";
                                data_.class="item item_hr"
                                this.msg.push(data_);
                                this.step=1;
                            })
                        }
                       
                    }else if(this.step==1){
                        this.sCode3=this.input;
                        let data_={};
                        data_.text="条码："+this.sCode3;
                        data_.class="item2"
                        this.msg.push(data_);
                        
                       /* data_={};
                        data_.text="正在进行火化调度，请稍等...";
                        data_.class="item2"
                        this.msg.push(data_);

                        this.$post(this.appid,"byg/cremated/setCrematedQueue2",{sCode:this.sCode,sCode2:this.sCode2,sCode3:this.sCode3}).then(data=>{
                            let data_={};
                            data_.text=unescape(data[0].msg);
                            if(data[0].status=="+OK"){
                                data_.class="item3";
                                this.msg.push(data_);
                               
                                data_={};
                                data_.type="goSearch";
                                data_.url=this.href;
                                //parent.postMessage(JSON.stringify(data_),'*');
                                this.$emit("goSearch",JSON.stringify(data_))
                                
                                data_={};
                                data_.text="请扫描逝者二维码开始火化...";
                                data_.class="item item_hr"
                                this.msg.push(data_);
                                this.step=1;
                                this.sCode="";
                                this.sCode2="";
                                this.sCode3="";
                            }else if(data[0].status=="LQ"){
                                this.isLQ=true;
                                data_={};
                                data_.text="该逝者已火化完成，正转入取灰操作...";
                                data_.class="item2"
                                this.msg.push(data_);

                                data_={};
                                data_.text="请扫描骨灰盒标签二维码进行取灰操作...";
                                data_.class="item item_hr"
                                this.msg.push(data_);
                            }else{

                                data_.class="item3";
                                this.msg.push(data_);
                                
                                data_={};
                                data_.text="请扫描逝者二维码开始火化...";
                                data_.class="item3 item_hr"
                                this.msg.push(data_);
                                this.step=1;
                               
                            }
                            this.step==1;
                            this.type="start";

                        
                        });*/

                        /*this.$post(this.appid,"byg/cremated/setCrematedQueueCheck",{
                            sCode:this.sCode3,
                            type:"sz"
                        }).then(data=>{
                            if(data[0].status=="+OK"){
                                data_={};
                                data_.text="请扫描工号...";
                                data_.class="item"
                                this.msg.push(data_);
                                this.step=2;
                            }else if(data[0].status=="LQ"){
                                this.isLQ=true;
                                data_={};
                                data_.text="请扫描骨灰盒标签二维码进行取灰操作...";
                                data_.class="item"
                                this.msg.push(data_);
                            }else{
                                data_={};
                                data_.text=unescape(data[0].msg)+";请重新扫描逝者二维码";
                                data_.class="item3"
                                this.msg.push(data_);
                            }
                        })*/
                        data_={};
                        data_.text="请扫描工号...";
                        data_.class="item"
                        this.msg.push(data_);
                        this.step=2;
                    }else if(this.step==2){
                        this.sCode=this.input;
                        let data_={};
                        data_.text="工号："+this.sCode;
                        data_.class="item2"
                        this.msg.push(data_);

                        /*this.$post(this.appid,"byg/cremated/setCrematedQueueCheck",{
                            sCode:this.sCode,
                            type:"user"
                        }).then(data=>{
                            if(data[0].status=="+OK"){
                                data_={};
                                data_.text="请扫描炉号二维码...";
                                data_.class="item"
                                this.msg.push(data_);
                                this.step=3;
                            }else{
                                data_={};
                                data_.text=unescape(data[0].msg)+";请重新扫描工号二维码";
                                data_.class="item3"
                                this.msg.push(data_);
                            }
                        })*/

                        /*this.$post(this.appid,"byg/cremated/setCrematedQueue2",{sCode:this.sCode,sCode2:this.sCode2,sCode3:this.sCode3}).then(data=>{
                            let data_={};
                            data_.text=unescape(data[0].msg);
                            if(data[0].status=="+OK"){
                                data_.class="item3";
                                this.msg.push(data_);
                               
                                data_={};
                                data_.type="goSearch";
                                data_.url=this.href;
                                //parent.postMessage(JSON.stringify(data_),'*');
                                this.$emit("goSearch",JSON.stringify(data_))
                                
                                data_={};
                                data_.text="请扫描逝者二维码开始火化...";
                                data_.class="item"
                                this.msg.push(data_);
                                this.step=1;
                            }else{

                                data_.class="item3";
                                this.msg.push(data_);
                                if(data[0].status3=="1"){
                                    data_={};
                                    data_.text="请重新扫描工号二维码...";
                                    data_.class="item"
                                    this.msg.push(data_);
                                }else{
                                    data_={};
                                    data_.text="请扫描逝者二维码开始火化...";
                                    data_.class="item3"
                                    this.msg.push(data_);
                                    this.step=1;
                                }
                            }
                            this.step==1;
                            this.type="start";

                        
                        });*/
                        data_={};
                        data_.text="请扫描炉号...";
                        data_.class="item"
                        this.msg.push(data_);
                        this.step=3;
                       
                    }else if(this.step==3){
                        this.sCode2=this.input;
                        let data_={};
                        data_.text="炉号："+this.sCode2;
                        data_.class="item2"
                        this.msg.push(data_);
                        data_={};
                        data_.text="正在进行火化调度，请稍等...";
                        data_.class="item2"
                        this.msg.push(data_);
                        
                        this.$post(this.appid,"byg/cremated/setCrematedQueue2",{sCode:this.sCode,sCode2:this.sCode2,sCode3:this.sCode3}).then(data=>{
                            let data_={};
                            data_.text=unescape(data[0].msg);
                            if(data[0].status=="+OK"){
                                data_.class="item3";
                                this.msg.push(data_);
                               
                                data_={};
                                data_.type="goSearch";
                                data_.url=this.href;
                                //parent.postMessage(JSON.stringify(data_),'*');
                                this.$emit("goSearch",JSON.stringify(data_))
                                
                                data_={};
                                data_.text="请扫描逝者二维码开始火化...";
                                data_.class="item"
                                this.msg.push(data_);
                                this.step=1;
                            }else{

                                data_.class="item3";
                                this.msg.push(data_);
                                if(data[0].status3=="1"){
                                    data_={};
                                    data_.text="请重新扫描炉号二维码...";
                                    data_.class="item"
                                    this.msg.push(data_);
                                }else{
                                    data_={};
                                    data_.text="请扫描逝者二维码开始火化...";
                                    data_.class="item3"
                                    this.msg.push(data_);
                                    this.step=1;
                                }
                            }
                            this.step==1;
                            this.type="start";

                        
                        });
                        
                    }
                }
            }
            this.input="";
        },
        closes(){
            let data_={};
            data_.type="close";
            this.$emit("closeWin",JSON.stringify(data_))
            parent.postMessage(JSON.stringify(data_),'*');
        }
    }
}
</script>
<style scoped>
.crematedScane{
    width:100%;
    height:100%;
    display:flex;
    flex-direction: column;
    overflow:hidden;
}
.content{
    flex:1;
    padding:10px;
    overflow:auto;
}
.footer{
    height:40px;
    padding:10px;
    border-top:1px solid #ddd;
    display:flex;
    flex-direction: row;
}
.sn{
    width:280px;

}
.button{
    text-align: center;
    flex:1;
}
.item{
    color:rgb(107, 119, 224);
}
.item_hr{
    margin-top:10px;
    border-top:1px solid blue;
}
.item2{
    color:green;
}
.item3{
    color:red;
    font-size:20px;
}
</style>